
#include <stdio.h>
#include "led.h"
#include "device.h"


static  sj_int8  led_device_open(struct file *file_node)
{
    sj_int8 ret =0;
    return ret;
}

static  sj_int8  led_device_write(struct file *file_node,sj_uint8 *data ,sj_int32 len,sj_loff_t * loff)
{
    struct led_device_data *self_data = (struct led_device_data *)(file_node->private_data);
    struct gpio_pin *pin =&self_data->res;
    sj_int8 ret =-1;   
    if(len>0)
    {
        if(data[0]=='1')
        {
            sj_gpio_write(pin,1);
        }
        else
        {
            sj_gpio_write(pin,0);
        }
    }

    return ret;
}

static  sj_int8  led_device_read(struct file *file_node,sj_uint8 *data ,sj_int32 len,sj_loff_t * loff)
{
    return 0;
}

static  sj_int8  led_device_close(struct file *file_node)
{
    sj_int8 ret =0;
    struct led_device_data *self_data = (struct led_device_data *)(file_node->private_data);

    return  ret;
}

static  sj_int8  led_device_flush(struct file *file_node)
{
    sj_int8 ret =0;
    struct led_device_data *self_data = (struct led_device_data *)(file_node->private_data);

    return ret;
}

static  sj_int8  led_device_poll(struct file *file_node,sj_int32 timeout)
{
    sj_int8 ret =0;

    return ret;
}

static  sj_int8  led_device_seek(struct file *file_node,sj_uint8 index ,sj_uint8 cur_set)
{
    sj_int8 ret =0;

    return ret;
}

static sj_void led_timer_callback(sj_void *arg)
{
    struct led_device_data *self_data =(struct led_device_data *)arg;

    struct gpio_pin *pin =&self_data->res;
    if(self_data->default_val)
    {
        self_data->default_val=0;
        sj_gpio_write(pin,self_data->default_val);
    }else{
        self_data->default_val=1;
        sj_gpio_write(pin,self_data->default_val);
    }
}
static sj_int8 led_device_probe(struct device *dev)
{
    sj_int8 ret =0;
    sj_int8 i=0;
    struct led_device_data *self_data =(struct led_device_data *)dev->private_data;
    if(!self_data)
    {
        ret =-1;
        goto out;
    }
    struct rcc_apb_periph *apb= &self_data->rcc_apb;
    struct gpio_pin *pin =&self_data->res;

    sj_gpio_clock_config(apb);

    sj_gpio_init(pin);

    sj_gpio_write(pin,self_data->default_val);

    if(self_data->tim_num>=0 &&self_data->call_id>=0)
    {
        nvic_set_tim_callback(self_data->tim_num,self_data->call_id,led_timer_callback,self_data);
    }

out:
    return ret;
}

static sj_int8 led_device_ioctl(struct file *file_node,sj_int8 cmd ,sj_uint8 *data)
{
    sj_int8 ret =0;

    switch(cmd)
    {

        default:
            break;
    }

    return ret;
}

static struct file_operations led_opt =
{
    led_device_open,
    led_device_close,
    led_device_flush,
    led_device_read,
    led_device_write,
    led_device_seek,
    led_device_poll,
    led_device_ioctl,
};

static struct device_id_table led_id_table[]=
{
    {"led"},
    {NULL}
};

static struct device_driver led_driver={
    "led",
    led_id_table,
    &led_opt,
    led_device_probe,
    NULL,
    NULL,
    NULL,
    NULL,
};


static DRIVER_MODULE_INIT(led_driver)

